package com.senseilearning.sensei;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.actionbarsherlock.app.SherlockFragment;

public class GridViewFragment extends SherlockFragment {
	//private String mContent = "???";
	private static final String KEY_TAB_NUM = "key.tab.num";
	private GridView gridView;
	private ChapterAdapter adapter;
	private int chapterId;
	
	public static GridViewFragment newInstance(String text) {
		GridViewFragment fragment = new GridViewFragment();
        
        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putString(KEY_TAB_NUM, text);
        fragment.setArguments(args);

        
        return fragment;
    }
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.chapters_grid, null);
        
		gridView = (GridView) view.findViewById(R.id.gridViewChapter);
		//adapter = new ChapterAdapter(this, test);
		
		Bundle bundle = this.getArguments();
		chapterId = bundle.getInt("chapter",0);
		
		adapter = new ChapterAdapter(getActivity(), Chapters.getSectionNames(chapterId));
		gridView.setAdapter(adapter);

		gridView.setDrawSelectorOnTop(true);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				Intent intent = new Intent(getActivity().getBaseContext(), GamesSelectionActivity.class);
				intent.putExtra("chapter", chapterId);
				intent.putExtra("section", position);
				startActivity(intent);
			}
		});
		return view;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//mContent =  getArguments() != null ? getArguments().getString(KEY_TAB_NUM) : "???";
	}
	
	public class ChapterAdapter extends BaseAdapter {
		private Context context;
		private ArrayList<String> chapterNames;

		public ChapterAdapter(Context context, ArrayList<String> chapterNames) {
			this.context = context;
			this.chapterNames = chapterNames;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View gridItem;
			gridItem = new View(context);
			gridItem = inflater.inflate(R.layout.chapter, null);
			TextView textView = (TextView) gridItem.findViewById(R.id.chapter_name);
			AspectRatioImageView imageView = (AspectRatioImageView) gridItem.findViewById(R.id.myImageView);
			try{
				String uri = "drawable/pics_c"+(chapterId+1)+"s"+(position+1);
				int imageResource = getResources().getIdentifier(uri, null, getActivity().getPackageName());
				Drawable image = getResources().getDrawable(imageResource);
				imageView.setImageDrawable(image);
			} catch(Exception ex){
				int p = position;
				while (p > 11) {
					p = p - 12;
				}
				String uri = "drawable/pics_"+(p+1);
				System.out.println(uri);
				int imageResource = getResources().getIdentifier(uri, null, getActivity().getPackageName());
				Drawable image = getResources().getDrawable(imageResource);
				imageView.setImageDrawable(image);
			}
			
			textView.setText(chapterNames.get(position));
			return gridItem;
		}

		public int getCount() {
			return chapterNames.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return position;
		}

	}
}
